define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'proxy/agent/index',
                    add_url: 'proxy/agent/add',
                    edit_url: 'proxy/agent/edit',
                    del_url: 'proxy/agent/del',
                    multi_url: 'proxy/agent/multi',
                    table: 'collectionagent',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'ops_collectionagent.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'ops_collectionagent.id', title: __('Id'),formatter:function(value,row,index){
                            return row.id;
                        }},
                        {field: 'ops_collectionagent.name', title: __('Name'),operate:'LIKE',placeholder: __('模糊查询'),formatter:function(value,row,index){
                            return row.name;
                        }},
                        {field: 'ops_collectionagent.content', title: __('Content'),operate:'LIKE',placeholder: __('模糊查询'),formatter:function(value,row,index){
                            return row.content;
                        }},
                        {field: 'e.name',title:__('局点名称'),operate:'LIKE',placeholder: __('模糊查询'),formatter:function(value,row,index){
                            return row.region_name;
                        }},
                        {field: 'ops_collectionagent.ip', title: __('Ip'),formatter:function(value,row,index){
                            return row.ip;
                        }},
                        {field: 'ops_collectionagent.port', title: __('ICMP端口'),formatter:function(value,row,index){
                            return row.port;
                        }},
                        {field: 'device_count', title: __('设备总数'),operate:false},
                        {field: 'd.name', title: __('Devices'),operate:'LIKE',placeholder: __('模糊查询'),formatter : Controller.api.formatter.tag,table: table, events:
                            {'click .labelhref':function(e,value,row){
                                var ids = $(this).attr('tag_iff'); 
                                // console.log(ids);                         
                                    Backend.api.closetabs('/admin/region/device/index?ids='+ids);
                                    Backend.api.addtabs('/admin/region/device/index?ids='+ids,'设备监控','fa fa-database');
                                }
                            }},                                            
                        {field: 'ops_collectionagent.createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,formatter:function(value,row,index){
                            return row.createtime;
                        }},
                        {field: 'ops_collectionagent.updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,formatter:function(value,row,index){
                            return row.updatetime;
                        }},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons: [
                            {
                                name: 'addtabs',
                                text: '',
                                title: 'ICMP配置',
                                classname: 'btn btn-xs btn-info btn-addtabs',                                
                                icon: 'fa fa-space-shuttle',                                
                                // icon: 'fa fa-folder-o',                              
                                url: 'collectionagent/icmp/index?collectionagent_id=',
                                visible: function (row,full) {
                                    full.url ='collectionagent/icmp/index?collectionagent_id='+row.id;
                                    return true;
                                 }
                            },
                            {
                                name: 'addtabs',
                                text: '',
                                title: 'ICMP告警配置',
                                classname: 'btn btn-xs btn-info btn-addtabs',                                
                                icon: 'fa fa-bell',                                
                                // icon: 'fa fa-folder-o',                              
                                url: 'collectionagent/alarm/index?collectionagent_id=',
                                visible: function (row,full) {
                                    full.url ='collectionagent/alarm/index?collectionagent_id='+row.id;
                                    return true;
                                 }
                            }
                            ]
                        }
                    ]
                ],
                queryParams: function (params) {
                    // console.log(Fast.api.query('ids'));
                    // return false;
                    if(Fast.api.query('ids')){
                        var filter = JSON.parse(params.filter);
                        var op = JSON.parse(params.op);
                        // 这里可以动态赋值，比如从URL中获取admin_id的值，
                        filter['ops_collectionagent.id']=Fast.api.query('ids');
                        op['ops_collectionagent.id'] = "=";
                        params.filter = JSON.stringify(filter);
                        params.op = JSON.stringify(op);
                    }
                    return params;
                }
            });

            

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $("#save").click(function(){
                var selectedComs = document.getElementById("groups_left");
                for(var i=0;i<selectedComs.length;i++){
                    selectedComs.options[i].selected = true;
                }
                return true;
            })
            $("#add").click(function(formname="web.hosts.host.php", objname="groups", from="groups_right", to="groups_left", action="add"){
                to = jQuery('#' + to);
                jQuery('#' + from).find('option:selected').each(function(i, fromel) {
                    var notApp = true;
                    to.find('option').each(function(j, toel) {
                        if (toel.innerHTML.toLowerCase() > fromel.innerHTML.toLowerCase()) {
                            jQuery(toel).before(fromel);
                            notApp = false;
                            return false;
                        }
                    });
                    if (notApp) {
                        to.append(fromel);
                    }
                    fromel = jQuery(fromel);
                    if (action.toLowerCase() == 'add') {
                        jQuery(document.forms[formname]).append("<input name='" + objname + '[' + fromel.val() + ']'
                            + "' id='" + objname + '_' + fromel.val() + "' value='" + fromel.val() + "' type='hidden'>");
                    }
                    else if (action.toLowerCase() == 'rmv') {
                        jQuery('#' + objname + '_' + fromel.val()).remove();
                    }
                });

                return true;
            });
            $("#remove").click(function(formname="web.hosts.host.php", objname="groups", from="groups_left", to="groups_right", action="rmv"){
                to = jQuery('#' + to);
                jQuery('#' + from).find('option:selected').each(function(i, fromel) {
                    var notApp = true;
                    to.find('option').each(function(j, toel) {
                        if (toel.innerHTML.toLowerCase() > fromel.innerHTML.toLowerCase()) {
                            jQuery(toel).before(fromel);
                            notApp = false;
                            return false;
                        }
                    });
                    if (notApp) {
                        to.append(fromel);
                    }
                    fromel = jQuery(fromel);
                    if (action.toLowerCase() == 'add') {
                        jQuery(document.forms[formname]).append("<input name='" + objname + '[' + fromel.val() + ']'
                            + "' id='" + objname + '_' + fromel.val() + "' value='" + fromel.val() + "' type='hidden'>");
                    }
                    else if (action.toLowerCase() == 'rmv') {
                        jQuery('#' + objname + '_' + fromel.val()).remove();
                    }
                });

                return true;
            });
        },
        edit: function () {
            Controller.api.bindevent();
            $("#save").click(function(){
                var selectedComs = document.getElementById("groups_left");
                for(var i=0;i<selectedComs.length;i++){
                    selectedComs.options[i].selected = true;
                }
                return true;
            })
            $("#add").click(function(formname="web.hosts.host.php", objname="groups", from="groups_right", to="groups_left", action="add"){
                to = jQuery('#' + to);
                jQuery('#' + from).find('option:selected').each(function(i, fromel) {
                    var notApp = true;
                    to.find('option').each(function(j, toel) {
                        if (toel.innerHTML.toLowerCase() > fromel.innerHTML.toLowerCase()) {
                            jQuery(toel).before(fromel);
                            notApp = false;
                            return false;
                        }
                    });
                    if (notApp) {
                        to.append(fromel);
                    }
                    fromel = jQuery(fromel);
                    if (action.toLowerCase() == 'add') {
                        jQuery(document.forms[formname]).append("<input name='" + objname + '[' + fromel.val() + ']'
                            + "' id='" + objname + '_' + fromel.val() + "' value='" + fromel.val() + "' type='hidden'>");
                    }
                    else if (action.toLowerCase() == 'rmv') {
                        jQuery('#' + objname + '_' + fromel.val()).remove();
                    }
                });

                return true;
            });
            $("#remove").click(function(formname="web.hosts.host.php", objname="groups", from="groups_left", to="groups_right", action="rmv"){
                to = jQuery('#' + to);
                jQuery('#' + from).find('option:selected').each(function(i, fromel) {
                    var notApp = true;
                    to.find('option').each(function(j, toel) {
                        if (toel.innerHTML.toLowerCase() > fromel.innerHTML.toLowerCase()) {
                            jQuery(toel).before(fromel);
                            notApp = false;
                            return false;
                        }
                    });
                    if (notApp) {
                        to.append(fromel);
                    }
                    fromel = jQuery(fromel);
                    if (action.toLowerCase() == 'add') {
                        jQuery(document.forms[formname]).append("<input name='" + objname + '[' + fromel.val() + ']'
                            + "' id='" + objname + '_' + fromel.val() + "' value='" + fromel.val() + "' type='hidden'>");
                    }
                    else if (action.toLowerCase() == 'rmv') {
                        jQuery('#' + objname + '_' + fromel.val()).remove();
                    }
                });

                return true;
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                tag: function (value, row, index) {                    
                    value = row.device_name === null ? '-' : row.device_name.toString();
                    id = row.device_id === null ? '' : row.device_id.toString();                 
                    //渲染Type
                    var html = [];
                    var arr = value.split(',');
                    var id_arr = id.split(',');                                                  
                    $.each(arr, function (i, value) {

                        value = value === null ? '' : value.toString();
                        if (value == '')
                            return true;
                        var color = 'primary';
                        var display = value;
                        // console.log(display);                       
                        if(display!=null&&display!=''&&display!='-'){                            
                            var tag_iff=id_arr[i];                            
                            html.push('<a title="点击查看详情" class="labelhref" href="#" name="'+value+'" tag_iff="'+tag_iff+'"><span class="label label-' + color + '" style="font-size:90%; line-height:2">' + value + '</span></a><br/>');
                        }else{
                            // html.push('<span title="点击查看详情" class="label label-' + color + '" style="font-size:90%;">' + display + '</span>');
                            html.push(display);
                        }
                    });
                    return html.join(' ');
                }
            }
        }
    };
    return Controller;
});